package com.wqtz.chechai.view.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.wqtz.chechai.R;
import com.wqtz.chechai.adapter.ImagesAdapter;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.base.rebaseadapter.MyHolder;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.SPUtils;
import com.wqtz.chechai.utils.ToastUtils;
import com.wqtz.chechai.widget.SpacesItemDecoration;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2017/1/5 16:14
 */

public class DriverBook1Activity extends BaseActivity {
	@BindView(R.id.iv_back)
	ImageView mIvBack;
	@BindView(R.id.title)
	TextView mTitle;
	@BindView(R.id.cancle)
	Button mCancle;
	@BindView(R.id.btn_confirm)
	Button mBtnConfirm;
	@BindView(R.id.rv_images)
	RecyclerView mRvImages;
	private ArrayList<String> defaultDataArray;
	private final int REQUEST_IMAGE_1 = 1010;//获取正本图片
	ImagesAdapter mAdapter;
	private ArrayList<String> imagespath;

	@Override
	public void init() {
		ButterKnife.bind(this);
		imagespath = new ArrayList<>();
		imagespath.add("default");//加号
		mAdapter = new ImagesAdapter(R.layout.item_image_selected, imagespath);
		mRvImages.setLayoutManager(new GridLayoutManager(this,3));
		int spacingInPixels = 8;
		mRvImages.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
		mRvImages.setAdapter(mAdapter);
		mAdapter.setDelOnClickCallBack(new ImagesAdapter.delOnClickCallBack() {
			@Override
			public void onDelClick(int position) {
				if(position==0){
					getPhoto1();
				}else {
					imagespath.remove(position);
					mAdapter.notifyDataSetChanged();
				}
			}
		});

	}

	@Override
	public void loadData() {

	}

	@Override
	public int setContentViews() {
		return R.layout.activity_carbook1;
	}

	@Override
	public BasePresent createPresenter() {
		return null;
	}


	@OnClick({R.id.cancle, R.id.btn_confirm})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.cancle:
				finish();
				break;
			case R.id.btn_confirm:
				if(imagespath.size()==1){
					ToastUtils.show(this,"您未选择任何图片",2000);}else {
					Intent intent = new Intent();
					intent.putStringArrayListExtra("images1",imagespath);
					setResult(RESULT_OK,intent);
					finish();
					}
				break;
		}
	}

	public void getPhoto1() {
		Intent intent = new Intent(this, MultiImageSelectorActivity.class);
		defaultDataArray = new ArrayList<>();
		// whether show camera
		intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
		// max select image amount
		intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 9);
		// select mode (MultiImageSelectorActivity.MODE_SINGLE OR MultiImageSelectorActivity.MODE_MULTI)
		intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
		// default select images (support array list)
		intent.putStringArrayListExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, defaultDataArray);
		startActivityForResult(intent, REQUEST_IMAGE_1);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
				case REQUEST_IMAGE_1: {
					// Get the result list of select image paths
					imagespath.addAll(data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT));
					SPUtils.put(this,"images",imagespath);
					//ToastUtils.show(this,imagespath.get(0),2000);
					mAdapter.notifyDataSetChanged();
				}
				break;
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		activitys.add(this); //添加当前的activity
	}

}
